var path = require('path'),
    webpack = require('webpack'),
    config = require('./config'),
    srcDir = path.join(__dirname, 'src'),
    imagePath = path.join(srcDir, 'img'),
    cssPath = path.join(srcDir, 'css'),
    nodeModulesPath = path.join(__dirname, 'node_modules'),
    autoprefixer = require('autoprefixer'),
    publicPath = config.domain + '/',
    HappyPack = require('happypack'),
    CopyWebpackPlugin = require('copy-webpack-plugin'),
    os = require('os'),
    CleanPlugin = require('clean-webpack-plugin'),
    HtmlWebpackPlugin = require('html-webpack-plugin'),
    happyThreadPool = HappyPack.ThreadPool({
        size: os.cpus().length
    }),
    CommonsChunkPlugin = require("webpack/lib/optimize/CommonsChunkPlugin");

module.exports = {
    cache: true,
    entry: {
        index: path.join(__dirname, "src/views/index.jsx"),
    },
    root: srcDir,
    output: {
        publicPath: publicPath,
        path: path.join(__dirname, "build/static/"),
        filename: "core/[name].js",
        chunkFilename: "core/[name].js"
    },
    resolve: {
        extensions: ['', '.js', '.jsx', '.json'],
        alias: {
            'img': path.join(srcDir, 'img'),
            'css': path.join(srcDir, 'css'),
            'js': path.join(srcDir, 'js'),
            'views': path.join(srcDir, 'views')
        },
        modulesDirectories: ["node_modules"]
    },
    module: {
        noParse: [/jquery/],
        loaders: [{
            test: /\.(png|jpg|gif)$/,
            include: [imagePath],
            loader: "url-loader?limit=1000&name=img/[name].[hash:5].[ext]"
        }, {
            test: /\.css$/,
            loader: "style!css!postcss"
        }, {
            test: /\.scss$/,
            loaders: ["style-loader", "css-loader?sourceMap", "postcss-loader", "sass-loader?sourceMap"],
        }, {
            test: /\.jsx$/,
            exclude: [nodeModulesPath],
            include: [srcDir],
            loader: "babel-loader",
            query: {
                presets: ['react', 'es2015'],
                "plugins": [
                    ["import", {
                        "libraryName": "antd-mobile",
                        "style": "css"
                    }]
                ]
            }
        }, {
            test: /\.js$/,
            exclude: [nodeModulesPath],
            include: [srcDir],
            loader: "babel-loader",
            query: {
                presets: ['es2015']
            }
        }]
    },
    postcss: [autoprefixer],
    sassLoader: {
        imagePath: imagePath,
        includePaths: [cssPath]
    },
    plugins: [
        new HappyPack({
            id: 'happybabel',
            loaders: ['babel-loader'],
            threadPool: happyThreadPool,
            cache: true,
            verbose: true
        }),
        new webpack.DllReferencePlugin({
            context: __dirname,
            manifest: require('./build/static/vendor-manifest.json')
        }),
        new CopyWebpackPlugin([{
            from: path.join(__dirname, 'src/js/lib'),
            to: path.join(__dirname, 'build/static/lib')
        }]),
        new HtmlWebpackPlugin({
            filename: path.join(__dirname, 'build/html/index.html'),
            template: path.join(__dirname, 'build/html/index_dll.html'),
            inject: true
        })
    ]
};